package com.longma.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longma.server.constant.enums.ResponseCode;
import com.longma.server.dao.OperationLogDAO;
import com.longma.server.entity.OperationLog;
import com.longma.server.exception.BusinessException;
import com.longma.server.pojo.dto.AdvanceQueryConditionDTO;
import com.longma.server.pojo.dto.OperationLogDTO;
import com.longma.server.pojo.dto.OperationLogUpdateDTO;
import com.longma.server.service.IOperationLogService;
import com.longma.server.util.MybatisPlusPageUtil;
import com.longma.server.util.WrappersUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class OperationLogServiceImpl implements IOperationLogService {
    @Autowired
    OperationLogDAO operationLogDAO;

    /**
     * 新增操作日志
     */
    @Override
    public OperationLog addOperationLog(OperationLogDTO addRequestDTO) {
        OperationLog operationLog = new OperationLog();
        BeanUtil.copyProperties(addRequestDTO,operationLog);
        operationLogDAO.save(operationLog);
        return operationLog;
    }

    /**
     * 根据id列表删除操作日志
     */
    @Override
    public boolean deleteOperationLogByIds(List<Integer> ids) {
        if (!isExistIds(ids)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        return operationLogDAO.removeByIds(ids);
    }

    /**
     * 根据id删除操作日志
     */
    @Override
    public boolean deleteOperationLogById(Integer id) {
        if (!isExistId(id)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }
        return operationLogDAO.removeById(id);
    }
    /**
     * 分页查询 获取操作日志
     */
    @Override
    public IPage<OperationLog> getOperationLogByPage(Long pageNum, Long pageSize, Boolean all, List<AdvanceQueryConditionDTO> conditions) {
        LambdaQueryWrapper<OperationLog> wrapper = WrappersUtil.lambdaQuery(conditions, OperationLog.class);
        Page<OperationLog> pageOperationLog = MybatisPlusPageUtil.getPage(pageNum, pageSize, all);
        return operationLogDAO.page(pageOperationLog, wrapper);
    }
    /**
     * 获取操作时间列表
     */
    public List<LocalDateTime> getOperationTimes(){
        List<OperationLog> operationLogs = operationLogDAO.list(Wrappers.<OperationLog>lambdaQuery().select());
        return operationLogs.stream().map(OperationLog::getOperationTime).collect(Collectors.toList());
    }
    /**
     * 更新操作日志
     */
    @Override
    public OperationLog updateOperationLog(OperationLogUpdateDTO updateRequestDTO) {
        // 存在校验
        if(operationLogDAO.getById(updateRequestDTO.getId()) == null){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        // 复制
        OperationLog operationLog = new OperationLog();
        BeanUtil.copyProperties(updateRequestDTO,operationLog);
        operationLogDAO.updateById(operationLog);
        return operationLog;
    }

    /**
     * 判断id列表是否存在
     */
    private boolean isExistIds(List<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return false;
        }
        LambdaQueryWrapper<OperationLog> wrapper = Wrappers.lambdaQuery();
        Set<Integer> productIds = new HashSet<>(ids);
        return operationLogDAO.count(wrapper.in(OperationLog::getId, productIds)) == productIds.size();
    }

    /**
     * 判断id是否存在
     */
    private boolean isExistId(Integer id) {
        return isExistIds(Arrays.asList(id));
    }
}
